<?php
// +----------------------------------------------------------------------
// | SentCMS [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.tensent.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: molong <molong@tensent.cn> <http://www.tensent.cn>
// +----------------------------------------------------------------------

namespace app\common\controller;

class Api {

	protected $data = array('code' => 0, 'msg' => '', 'time' => 0, 'data' => '');
	public $mustToken = false;     //是否检查用户行为
	protected $user      = array();    //用户信息
	protected $client;                //客户端信息
	protected $request;

	public function __construct(\think\Request $request) {
		$this->setHeader();
		$this->request = $request;
		$this->data['time'] = time();
		if ($this->request->isOptions()){
			exit('OK');
		}
		$header = $this->request->header();

		if (!$this->checkAuthor($header)) {    //检查客户端接口是否可接入
			$this->data['code'] = '301';
			$this->data['data'] = '非法请求！';
			echo json_encode($this->data);exit();
		}

		if ($this->mustToken) {
			if (!$this->checkToken($header)) {
				$this->data['code'] = '203';
				$this->data['data'] = '用户登录信息失效，请重登！';
				echo json_encode($this->data);exit();
			}
		}
	}

	protected function checkAuthor($header){
		if (isset($header['authorization']) && $header['authorization']) {
			list($appid, $sign) = explode('{|}', $header['authorization']);
			$this->client = db('Client')->where('appid', $appid)->find();
			if ($sign == md5($this->client['appid'].$this->client['appsecret'])) {
				return true;
			}else{
				return false;
			}
		}else{
			return false;
		}
	}

	protected function checkToken($header){
		if (isset($header['access_token']) && $header['access_token']) {
			$token = authcode($header['access_token']);
			list($uid, $username, $password) = explode('|', $token);
			$this->user = model('User')->where('uid', $uid)->where('username', $username)->find();
			if ($this->user && $password === $this->user['password']) {
				return true;
			}else{
				$this->user = array();
				return false;
			}
		}else{
			return false;
		}
	}

	protected function setHeader(){
		header("Access-Control-Allow-Origin: *");
		header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS");
		header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, Authorization, access_token");
	}

    /**
     * 基本数据验证
     * @param $data
     */
    public function setuser($data)
    {

        # username 用户名
        if (isset($data->username)) {
            $map['username'] = $data->username;
        } else {
            $this->json_error(102, 'user');
        }
        # pid 所属商户
        if (isset($data->pid)) {
            $map['pid'] = $data->pid;
        }
        # password  密码
        if (isset($data->password)) {
            $map['password'] = $data->password;
        }else{
            $this->json_error(102, 'password');
        }
        $map['salt'] = rand_string(6);
        # nickname 昵称
        $map['nickname'] = $data->nickname;
        $map['email'] = $data->email;
        if (isset($data->mobile)) {
            $map['mobile'] = $data->mobile;
        }else{
            $this->json_error(102, 'mobile');
        }
        $map['sex'] = $data->sex;
        $map['birthday'] = $data->birthday;
        $map['qq'] = $data->qq;
        $map['pos_province'] = $data->province;
        $map['pos_city'] = $data->city;
        $map['pos_district'] = $data->district;
        $map['pos_community'] = $data->community;
        $map['reg_ip'] = $data->ip;
        $map['reg_time'] = $data->time;
        $map['type'] = $data->type;
        if($data->business_name){
            $map['business_name'] = $data->business_name;
        }
        if($data->corporation){
            $map['corporation'] = $data->corporation;
        }
        if($data->bname){
            $map['bname'] = $data->bname;
        }
        if($data->industry){
            $map['industry'] = $data->industry;
        }
        if($data->business_license){
            $map['business_license'] = $data->business_license;
        }
        if($data->idcard){
            $map['idcard'] = $data->idcard;
        }
        $map['is_shop'] = $data->is_shop;
        if($data->id){
            $map['id'] = $data->id;
        }
        //var_dump($map);
        return $map;
    }
    /**
     * 基本数据验证
     * @param $data
     */
    public function setpro($data)
    {
        if($data->id){
            $map['id'] = $data->id;
        }
        # title 产品标题
        if (isset($data->title)) {
            $map['title'] = $data->title;
        } else {
            $this->json_error(102, 'title');
        }
        # price 产品价格
        $map['price'] = $data->price;
        # buying_price 产品进货价
        $map['buying_price'] = $data->buying_price;
        # trade_price 产品批发价
        $map['trade_price'] = $data->trade_price;
        # description 产品描述
        if($data->description){
            $map['description'] = $data->description;
        }else{
            $map['description']=msubstr_add($data->content,0,100);
        }
        if (isset($data->uid)) {
            $map['uid'] = $data->uid;
        } else {
            $map['uid'] = "1";
        }
        # category 产品分类
        if (isset($data->category)) {
            $map['category_id'] = $data->category;
        }
        # cover_id 产品封面图片
        if (isset($data->cover_id)) {
            $map['cover_id'] = $data->cover_id;
        }else{
            $map['cover_id']=0;
        }
        # img_list 产品图片
        if (isset($data->img_list)) {
            $map['img_list'] = $data->img_list;
        }else{
            $map['img_list'] = '';
        }
        # content 产品内容
        if ($data->content) {
            $map['content'] = $data->content;
        } else {
            $this->json_error(102, 'content');
        }
        # display_position 产品或新闻显示位置
        if (isset($data->display_position)) {
            $map['display_position'] = $data->display_position;
        } else {
            $this->json_error(102, 'display_position');
        }
        # is_top_web 产品置顶
        if (isset($data->is_top)) {
            $map['is_top'] = $data->is_top;
        }
        if (isset($data->unit)) {
            $map['unit'] = $data->unit;
        }
        if (isset($data->shelves_time)) {
            $map['shelves_time'] = $data->shelves_time;
        }
        if (isset($data->shelves)) {
            $map['shelves'] = $data->shelves;
        }
        //var_dump($map);
        return $map;
    }


    /**
     * 正确返回(JSON格式)
     * @param $code 错误编码
     */
    public function json_success($map = null)
    {
        $data['status'] = 'success';
        $data['code'] = 200;
        if (!empty($map)) {
            $data['data'] = $map;
        }
        exit(json_encode($data));
    }

    /**
     * 错误返回(JSON格式)
     * @param $code 错误编码
     */
    public function json_error($code, $param = false)
    {
        $data['status'] = 'error';
        $data['code'] = $code;
        if (!empty($param)) {
            $data['param'] = $param;
        }
        exit(json_encode($data));
    }

}